using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SimpleJson;
using System;
using FairyGUI;

namespace ABA
{
    public class SysLoading : Singleton<SysLoading>
    {
        private class LoadingWin : FGUIWindow
        {
            GProgressBar bar;
            GTextField tipsText;
            public LoadingWin(FGUIWinInfo type):base(type){
            }

            float posY = 0;
            protected override void OnInit()
            {
                base.OnInit();
                bar = contentPane.GetChild("bar").asProgress;
                posY = bar.y;
                tipsText = contentPane.GetChild("tips")?.asTextField;
            }

            double _min = 0;
            double _max = 0;
            string _version = "";
            float _delayShowBar = -1;
            public void ShowLoadingBar(double min, double max, string version, float delayShowBar = -1){  // 延迟多少s 显示进度条
                _min = min;
                _max = max;
                _version = version;
                _delayShowBar = delayShowBar;

                Show();
                if(tipsText != null){
                    tipsText.text = "";
                }
            }

            long _showBarTimer_id = 0;
            override protected void OnShown()
            {
                if(_version != "")
                    contentPane.asCom.GetChild("version").asTextField.text = _version;
                bar.min = _min;
                bar.max = _max;
                bar.value = 0;    

                GameTimer.inst.Remove(_showBarTimer_id);
                _showBarTimer_id = 0;
                if(_delayShowBar > 0){
                    HideBar();
                    _showBarTimer_id = GameTimer.inst.Add(_delayShowBar, 1, ShowBar);
                }
                else{
                    ShowBar(null);
                }
            }
            
            public bool UpdateLoadingBar(double cur, string tips){
                if(cur >= 0) bar.value = cur;
                if(tipsText != null)
                    tipsText.text = tips;
                if(bar.value >= bar.max){
                    return true;
                }
                return false;
            }

            void HideBar(){
                bar.y = posY + 1000;  // 移出屏幕外隐藏                   
            }

            void ShowBar(JsonObject p){
                bar.y = posY;
            }
        }

        
        LoadingWin wind;
        Action _completeCB;
        protected override void Init(){
            
            var SYS_LOADING = new FGUIWinInfo
            {
                cls_name = "LoadingWin",
                pkg_name = "sys",
                comp_name = "loading",
                zindex = E_FGUIWIN_ZINDEX.LOADING,
                ani = false
            };

            wind = new LoadingWin(SYS_LOADING);
            Event.inst.AddListener<double, string>(CommData.EVENT_LOADING_BAR_UPDATE, "Loading", _LoadingBarUpdate);       //LOADING_BAR_UPDATE            
        }

        private void _LoadingBarUpdate(double cur, string tips){
            bool b = wind.UpdateLoadingBar(cur, tips);
            if(b == true){
                if(_completeCB != null){
                    _completeCB();
                    _completeCB = null;
                }
            }
        }

        // 外部通过消息EventConst.LOADING_BAR_UPDATE更新进度条，外部回调里面主动调用 Hide关闭
        public void ShowLoadingBar(double min, double max, Action completeCB, string version = "", float delayShowBar = -1){
            _completeCB = completeCB;
            wind.ShowLoadingBar(min, max, version, delayShowBar);
        }

        public void Hide(bool force = false){

            wind.Hide();
        }
    }
}
